package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

import java.util.ArrayList;

/**
 * @author Xiao
 * @date 2021/7/19
 */
public class TeacherDao {
    static ArrayList<Teacher> list = new ArrayList<>();
    //静态代码块
    static{
        Teacher t1 = new Teacher("itheima001","张三",23,"19980808");
        Teacher t2 = new Teacher("itheima002","李四",24,"19970707");
        list.add(t1);
        list.add(t2);
    }

    public ArrayList<Teacher> findAllTeacher() {
        return list;
    }

    public boolean addTeacher(Teacher t) {
        return list.add(t);
    }

    public boolean deleteTeacher(String tid) {
        int index = getIndex(tid);
        if (index != -1) {
            list.remove(index);
            return true;
        }
        return false;
    }

    private final int getIndex(String tid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Teacher t = list.get(i);
            if (tid.equals(t.getId())) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(Teacher t, String tid) {
        int index = getIndex(tid);
        if (index != -1) {
            /*list.remove(index);
            list.add(index,t);*/
            list.set(index,t);
        }
    }
}
